package tech.tongyu.bob.demo.beanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("user")) {
            System.out.println("BeanPostProcessor#postProcessBeforeInitialization called for bean: " + beanName);
            User user = (User) bean;
            user.setName("Modified Name");
            user.setAge(25);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof User) {
            User user = (User) bean;
            user.displayInfo();
            System.out.println("BeanPostProcessor#postProcessAfterInitialization called for bean: " + beanName);
        }
        return bean;
    }
}
